package com.richest.api.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.richest.api.controller.form.LoginForm;
import com.richest.api.controller.form.PhoneForm;
import com.richest.api.service.IUserService;
import com.richest.api.utils.Constant;
import com.richest.api.utils.RedisUtils;
import com.richest.api.utils.SMSUtil;
import com.richest.bean.UserBean;
import com.richest.util.CallResults;
import com.richest.util.DateUtils;
import com.richest.util.MD5Util;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 登录接口 2019年3月12日 上午10:08:15 richest-api
 * 
 * @author zhaohongchao
 * @version 1.0 ApiLoginController.java
 */
@RestController
@RequestMapping("/loginController")
@Api(value = "登录接口")
@EnableAutoConfiguration
public class ApiLoginController {
	@Autowired
	private IUserService userService;

	@Autowired
	private RedisUtils redisUtils;

	@RequestMapping(value = "sign", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "登录", value = "登录", httpMethod = "POST")
	@ApiImplicitParam(name = "form", value = "登录", required = true, dataType = "LoginForm")
	public CallResults login(@RequestBody LoginForm form) {
		// 用户登录
		return userService.login(form);
	}

	@RequestMapping(value = "sendCode", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "发送短信验证码", value = "发送短信验证码", httpMethod = "GET")
	@ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query", dataType = "String")
	public CallResults sendCode(@RequestParam String phone) {
		// 表单校验
		if (redisUtils.get(Constant.SMS_OVERTIME_KEY + phone) != null) {
			return CallResults.error("发送验证码比较频繁，等一分钟之后再试试");
		}
		String number = RandomUtil.randomNumbers(6);
		SMSUtil.sendSMS(phone, "尊敬的用户：您的校验码：" + number + "，工作人员不会索取，请勿泄漏。", "0");
		redisUtils.set(Constant.SMS_CODE_KEY + phone, number, Constant.CODE_EXPIRE);
		redisUtils.set(Constant.SMS_OVERTIME_KEY + phone, number, Constant.CODE_EXPIRE);
		return CallResults.ok("验证码获取成功").put("data", number);
	}

	@RequestMapping(value = "speedLogin", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "快捷登陆", value = "快捷登陆", httpMethod = "POST")
	@ApiImplicitParam(name = "form", value = "登录", required = true, dataType = "PhoneForm")
	public CallResults speedLogin(@RequestBody PhoneForm form) {
		Assert.notBlank(form.getCode(), "短信验证码不能为空");
		String code = redisUtils.get(Constant.SMS_CODE_KEY + form.getPhone());
		if (code == null) {
			return CallResults.error("验证码已经失效，请重新获取");
		}
		if (!form.getCode().equals(code)) {
			return CallResults.error("输入验证码有误，请重新填写");
		}

		return userService.speedRegister(form);
	}

	@RequestMapping(value = "logout", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "退出", value = "退出", httpMethod = "GET")
	@ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "Long")
	public CallResults logout(@RequestParam Long userId) {
		redisUtils.delete("token" + userId);
		return CallResults.ok();
	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value = "forgetPass", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "忘记密码", value = "忘记密码", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "password", value = "新密码", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "confirmPassword", value = "确认密码", required = true, paramType = "query", dataType = "String") })
	public CallResults resetPass(@RequestParam String phone, @RequestParam String code, @RequestParam String password,
			@RequestParam String confirmPassword) {
		UserBean UserBean = userService.queryByMobile(phone);
		Assert.notBlank(code, "短信验证码不能为空");
		String codeRedis = redisUtils.get(Constant.RESET_PASS_SMS_CODE_KEY + phone);
		if (codeRedis == null) {
			return CallResults.error("验证码已经失效，请重新获取");
		}
		if (!code.equals(codeRedis)) {
			return CallResults.error("输入验证码有误，请重新填写");
		}

		if (password == null || "".equals(password)) {
			return CallResults.error("新密码不能为空");
		}
		if (confirmPassword == null || "".equals(confirmPassword)) {
			return CallResults.error("请确认新密码");
		}
		if (!password.equals(confirmPassword)) {
			return CallResults.error("二次密码输入不一致,请重新输入新密码");
		}
		UserBean.setPassword(MD5Util.string2MD5(password));
		UserBean.setUpdate_time(DateUtils.getNowTime());
		UserBean.setUpdate_by(UserBean.getPhone());
		userService.update(UserBean);
		return CallResults.ok("修改密码成功");
	}

}
